package com.education.platform.management.controller.sys;

import com.education.platform.common.enums.DicTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.entity.FeedbackDO;
import com.education.platform.dao.entity.SysDictionaryDO;
import com.education.platform.dao.model.FeedbackModel;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.enums.PushTypeEnum;
import com.education.platform.management.push.PushUtil;
import com.education.platform.management.service.FeedbackService;
import com.education.platform.management.service.SysDictionaryService;
import com.education.platform.management.service.UserBaseService;
import com.education.platform.management.vo.request.FeedbackQueryVO;
import com.education.platform.management.vo.request.FeedbackUpdateVO;
import com.education.platform.management.vo.response.FeedbackPageInfoVO;
import com.education.platform.management.vo.response.FeedbackResponseVO;
import com.education.platform.management.vo.response.SysDictionaryResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* describe: 意见反馈
* @author : tomato
* @date: 2020-06-29 10:28:07.517
**/
@RestController
@Api("意见反馈")
@RequestMapping("feedback")
@Validated
public class FeedbackController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FeedbackController.class);

    @Autowired
    private SysDictionaryService sysDictionaryService;
	@Autowired
    private FeedbackService feedbackService;
	@Autowired
    private UserBaseService userBaseService;
    @Autowired
    private PushUtil pushUtil;
    /**
     * 分页查询
     * @param feedbackQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("getPageList")
    public FeedbackPageInfoVO selectPageList(@RequestBody @Valid FeedbackQueryVO feedbackQueryVO) {
        String info = String.format("The method name[selectPageList] params:%s", feedbackQueryVO.toString());
        LOGGER.info(info);
        FeedbackModel feedback = BeanUtil.convert(feedbackQueryVO, FeedbackModel.class);

        Page<FeedbackResponseVO> page = PageHelper.startPage(feedbackQueryVO.getPageNum(), feedbackQueryVO.getPageSize());
        List<FeedbackModel> feedbackList = feedbackService.getFeedBackListBms(feedback);
        FeedbackPageInfoVO feedbackPageInfo = new FeedbackPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), feedbackPageInfo);
        feedbackPageInfo.setPageNum(feedbackQueryVO.getPageNum());
        List<FeedbackResponseVO> voList = new ArrayList<>();
        for(FeedbackDO back :feedbackList){
            FeedbackResponseVO responseVO = BeanUtil.convert(back,FeedbackResponseVO.class);
            if(StringUtils.isNotEmpty(back.getPicUrl())){
                responseVO.setUrls(Arrays.asList(back.getPicUrl().split(",")));
            }
            voList.add(responseVO);
        }
        feedbackPageInfo.setList(voList);
        return feedbackPageInfo;
    }



    /**
     * 回复
     * @param
     * @return 回复
     */
    @ApiOperation(value = "回复")
    @PostMapping("reply")
    public void reply(@RequestBody @Valid FeedbackUpdateVO updateVO) {
        FeedbackDO feedbackDO = feedbackService.getById(updateVO.getId());
        if(feedbackDO == null){
            throw new BusinessException("评论记录不存在");
        }
        if(StringUtils.isNotEmpty(feedbackDO.getReply())){
            throw new BusinessException("该反馈已回复过！");
        }
        FeedbackDO update = new FeedbackDO();
        update.setId(updateVO.getId());
        update.setReply(updateVO.getReply());
        update.setUpdateBy(ThreadUser.getUserName());
        update.setUpdateTime(new Date());
        feedbackService.updateById(update);
        //推送消息
//        PushUtil pushUtil = PushUtil.getInstance();
        pushUtil.push(feedbackDO.getUserId(), PushTypeEnum.FEED_BACK,String.valueOf(feedbackDO.getId()),null);
    }




    @ApiOperation(value = "获取问题类型")
    @PostMapping("selectProblemType")
    public List<SysDictionaryResponseVO> selectProblemType() {
        List<String> typeCodes = Arrays.asList(DicTypeEnum.PROBLEM_TYPE.getValue());
        List<SysDictionaryDO> dictionaryDTOS = sysDictionaryService.getListByDicTypeCodeList(typeCodes);
        List types =  dictionaryDTOS.parallelStream().filter(m->DicTypeEnum.PROBLEM_TYPE.getValue().equals(m.getTypeCode())).collect(Collectors.toList());
        types = BeanUtil.convert(types,SysDictionaryResponseVO.class);
        return types;
    }
}
